from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains as action
from selenium import webdriver
from selenium.common.exceptions import *

class PublicMethod(object):
    '''二次封装底层类'''
    def __init__(self,driver:webdriver.Chrome):
        self.driver = driver
        self.timeout = 5
        self.poll = 1

    def findElement(self,loctor):
        '''定位单个元素,返回元素对象'''
        element = WebDriverWait(self.driver,self.timeout,self.poll).until(EC.presence_of_element_located(loctor))
        return element

    def findElements(self,loctor):
        '''定位多个元素，返回元素对象'''
        elements = WebDriverWait(self.driver,self.timeout,self.poll).until(EC.presence_of_all_elements_located(loctor))
        return elements

    def title_is(self,title_text):
        '''定位页面标题是否相等，返回bool值'''
        return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.title_is(title_text))

    def title_contains(self,title_text):
        '''定位页面标题是否包含，返回bool值'''
        return  WebDriverWait(self.driver,self.timeout,self.poll).until(EC.title_contains(title_text))

    def visibility_of_element_located(self,loctor):
        '''判断某个元素是否可见. 可见代表元素非隐藏'''
        return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.visibility_of_element_located(loctor))

    def text_to_be_present_in_element(self,loctor,text):
        '''判断元素text是否包含了预期字符串,返回bool值'''
        try:
            return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.text_to_be_present_in_element(loctor,text))
        except TimeoutException:
            return False

    def text_to_be_present_in_element_value(self,loctor,value):
        '''判断某个元素中的value属性是否 包含了预期的字符串'''
        try:
            return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.text_to_be_present_in_element_value(loctor,value))
        except TimeoutException:
            return False

    def element_to_be_selected(self,loctor):
        '''判断某个元素是否被选中'''
        try:
            return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.element_to_be_selected(self.findElement(loctor)))
        except TimeoutException:
            return False

    def alert_is_present(self):
        '''判断页面上是否存在alert'''
        try:
            return WebDriverWait(self.driver,self.timeout,self.poll).until(EC.alert_is_present())
        except TimeoutException:
            return False

    def move_to_element(self,loctor):
        '''鼠标悬停操作'''
        ele = self.findElement(loctor)
        action(self.driver).move_to_element(ele).perform()

    def click(self,loctor):
        '''鼠标点击操作'''
        self.findElement(loctor).click()

    def sendKeys(self,loctor,text,is_full=False):
        '''写入文本，如果is_full为True先清空在写入'''
        ele = self.findElement(loctor)
        if is_full:
            ele.clear()
        ele.send_keys(text)

    def clear(self,loctor):
        '''清空文本'''
        return self.findElement(loctor).clear()
